/**
 * 客户资源录入脚本支持
 */

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	button_bind();
	init_city( "province", "city" );
} );

//--------------------------------------------------------------------------
//
//	consultant_client_first.php
//
//--------------------------------------------------------------------------

var _timeoutid = null;

function button_bind()
{
	$( "#btnSubmit" ).click( submit_form );

	// 搜索
	$( "#source" ).change( search_client );
	$( "#category" ).change( search_client );
	$( "#province" ).change( search_client );
	$( "#state" ).change( search_client );
	$( "#intention" ).change( search_client );
	$( "#city" ).change( search_client );
	$( "#address" ).keyup( search_client_keyup );
	$( "#nickname" ).keyup( search_client_keyup );
	
	$( "#phone" ).keyup( search_client_keyup );
	$( "#email" ).keyup( search_client_keyup );
	$( "#qq" ).keyup( search_client_keyup );

	// 结果中搜索
	$( "#nickname_key" ).keyup( search_inner_keyup );
}

function search_client_keyup( e )
{
	_timeoutid && clearTimeout( _timeoutid );
	_timeoutid = setTimeout( search_client, 300 );
}

function empty_list()
{
	$( "#clientlst" ).find( "ul" ).empty();
}

function search_client()
{
	// 还原，等下次利用
	_timeoutid = null;
	
	// 搜索条件
	var data = {};
	data.source = $( "#source" ).val();
	data.category = $( "#category" ).val();
	data.province = $( "#province" ).val();
	data.city = $( "#city" ).val();
	data.address = $( "#address" ).val();
	data.nickname = $( "#nickname" ).val();
	data.intention = $( "#intention" ).val();
	data.state = $( "#state" ).val();
	
	data.type=$("#type").val();
	data.phone = $( "#phone" ).val();
	data.email = $( "#email" ).val();
	data.qq = $( "#qq" ).val();

	if( empty( data.source ) && empty( data.category ) && empty( data.province ) && empty( data.city )
			&& empty( data.address ) && empty( data.nickname ) && empty( data.phone ) && empty( data.email )
			&& empty( data.qq ) && empty( data.intention ) && empty( data.state ))
	{
		empty_list();
	}
	else
	{
		submitter.post( contextPath+"/index/get_client_allot.json", data, function( d )
		{
			var user_name = $("#user_name").val();
			empty_list();
			var list = d.list;
			var objHtml = "";
			for(var i=0;i<list.length;i++){
				if((list[i].rawuser==null || list[i].rawuser=="") && (list[i].ueuser!=null && list[i].ueuser!="")){
					list[i].rawuser = list[i].ueuser;
				}
				if(list[i].rawuser == user_name){
					objHtml +="<li class=\"unit\" title=\"目前归属人：" + list[i].rawuser + "\">"
					+ "<input type=\"checkbox\" name='allcheck' id=\"" + list[i].id + "\"><label onclick=\"addFloat('"+list[i].keyname+"')\" for=\"" + list[i].keyname + "\">"
					+ "<span class=\"stress\">" + list[i].nickname + "</span> " + list[i].sourcens + "-"
					+ list[i].categoryns + "</label></li>";
				}else{
					objHtml +="<li class=\"unit\" title=\"目前归属人：" + list[i].rawuser + "\">"
					+ "<input type=\"checkbox\" name='allcheck' id=\"" + list[i].id + "\"><label onclick=\"addFloat('"+list[i].keyname+"')\" for=\"" + list[i].keyname + "\">"
					+ "<span class=\"stress\">" + list[i].nickname + "</span> " + list[i].sourcens + "-"
					+ list[i].categoryns + "</label></li>";
				}
			}
			$( "#clientlst" ).find( "ul" ).append( objHtml );
		}, "json" );
	}
}
//全选
$(function(){
	$("#controlAll").change(function(){
		var div=document.getElementById('clientlst');
		var  CheckBox=div.getElementsByTagName('input');
		
		if($("#controlAll").prop("checked")){
			for(var i=0;i<CheckBox.length;i++){
			    CheckBox[i].checked=true;
			    }
		}else{
			for(i=0;i<CheckBox.length;i++){
				CheckBox[i].checked=false;
				};	
			}
	});
});

var _timeoutid_inner = null;

function search_inner_keyup()
{
	_timeoutid_inner && clearTimeout( _timeoutid_inner );
	_timeoutid_inner = setTimeout( search_client_inner, 300 );
}

function search_client_inner()
{
	var key = $( "#nickname_key" ).val();
	var list = [];

	$( "#clientlst ul li" ).each( function()
	{
		var text = $( this ).find( "span" ).text();
		$( this ).removeClass( "selected" );

		if( !empty( key ) && text.indexOf( key ) > -1 )
			list.push( $( this ) );
	} );

	for( var i = 0; i < list.length; i++ )
	{
		var li = list[ i ];
		li.addClass( "selected" );
		$( "#clientlst ul" ).prepend( li );
	}
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function submit_form()
{
	var data = {};

	// 转交资源列表
	var cs = [];
	$( "#clientlst" ).find( "ul>li>:checked" ).each( function( id )
	{
		cs.push( $( this ).attr( "id" ) );
	} );
	cs = cs.join( "," );

	if( empty( cs ) ){
		alert( "请选择需要转交的客户资源。" );
		return false;
	}
	data.clients = cs;

	// 转交目标
	data.target = $( "#target" ).val();

	if( empty( data.target ) ){
		alert( "请选择转交目标咨询师。" );
		return false;
	}

	submitter.post( contextPath+"/index/system_client_allot_go.json", data, function( d )
	{
		alert("转接成功");
	}, "json" );
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
}
